//
//  FriendApplyVerifyViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/13.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "FriendApplyVerifyViewController.h"
#import "NewingFriendModel.h"
#import "NewFriendTableViewCell.h"
#import "FriendSetViewController.h"

@interface FriendApplyVerifyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NewingFriendModel *friendApplyModel;
@property (nonatomic, strong) UITableView *friendApplyTableView;

@end

@implementation FriendApplyVerifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加数据源
    [self addData];
    // 布局
    [self addMySubview];
}


// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"好友验证";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
 
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加数据源
- (void)addData
{
    NSLog(@"融云好友model = %@", self.friendDataModel);
    NSLog(@"本地好友dic = %@", self.benDiFriendData);
    if (self.benDiFriendData.count == 0) {
        NSLog(@"融云本地数据");
        // 说明是融云本地好友消息
        RCContactNotificationMessage *rcNM = (RCContactNotificationMessage *)self.friendDataModel.lastestMessage;
        NSString *friendMessage = rcNM.message;
        NSString *remarkString = [NSString stringWithFormat:@""];
        // 获取发送者姓名
        NSString *senderName = @"";
        // 进行模拟数据的组件
        if ([self.friendDataModel.senderUserId isEqualToString:@"10000"]) {
            senderName = @"张国文";
        } else if ([self.friendDataModel.senderUserId isEqualToString:@"10001"]) {
            senderName = @"梅江林";
        } else if ([self.friendDataModel.senderUserId isEqualToString:@"10002"]) {
            senderName = @"殷雄";
        } else if ([self.friendDataModel.senderUserId isEqualToString:@"10003"]) {
            senderName = @"吕峰";
        } else if ([self.friendDataModel.senderUserId isEqualToString:@"10004"]) {
            senderName = @"欧兆";
        } else if ([self.friendDataModel.senderUserId isEqualToString:@"10005"]) {
            senderName = @"沈佳";
        }
        if ([friendMessage isEqualToString:@"haha"]) {
            remarkString = [NSString stringWithFormat:@"我是%@，我想加你为好友", senderName];
        } else {
            // 说明发送者附加信息了
            remarkString = friendMessage;
        }
        
        NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:senderName, @"_name", remarkString, @"_mark", @"NO", @"_isHaveAgree", nil];
        // 建立模拟数据
        self.friendApplyModel = [[NewingFriendModel alloc] init];
        [self.friendApplyModel setValuesForKeysWithDictionary:tempDic];
    } else {
        // 说明是自己创建的本地好友数据库
        NSLog(@"自己创建的本地数据库");
        // 发送者姓名
        NSString *senderName = @"";
        // remark
        NSString *remarkString = @"";
        // 消息内容
        NSString *friendMessage = [self.benDiFriendData valueForKey:@"kSenderAddMessage"];
        // 发送者ID
        NSString *senderID = [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserIDDuiYingSenderIDByKey:[self.benDiFriendData valueForKey:@"senderID"]];
        // 进行模拟数据的组件
        if ([senderID isEqualToString:@"10000"]) {
            senderName = @"张国文";
        } else if ([senderID isEqualToString:@"10001"]) {
            senderName = @"梅江林";
        } else if ([senderID isEqualToString:@"10002"]) {
            senderName = @"殷雄";
        } else if ([senderID isEqualToString:@"10003"]) {
            senderName = @"吕峰";
        } else if ([senderID isEqualToString:@"10004"]) {
            senderName = @"欧兆";
        } else if ([senderID isEqualToString:@"10005"]) {
            senderName = @"沈佳";
        }
        if ([friendMessage isEqualToString:@"haha"]) {
            remarkString = [NSString stringWithFormat:@"我是%@，我想加你为好友", senderName];
        } else {
            // 说明发送者附加信息了
            remarkString = friendMessage;
        }
        NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:senderName, @"_name", remarkString, @"_mark", @"NO", @"_isHaveAgree", nil];
        // 建立模拟数据
        self.friendApplyModel = [[NewingFriendModel alloc] init];
        [self.friendApplyModel setValuesForKeysWithDictionary:tempDic];
    }
    
    
}

#pragma mark - 布局
- (void)addMySubview
{
    self.friendApplyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.friendApplyTableView.dataSource = self;
    self.friendApplyTableView.delegate = self;
    self.friendApplyTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_friendApplyTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"applyFriend";
    NewFriendTableViewCell *cell = [[NewFriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.friendApplyModel;
    [cell.agreeOrIngoreButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

#pragma mark - 好友申请点击同意
- (void)click:(UIButton *)button
{
    NSLog(@"好友申请同意");
    FriendSetViewController *friendSetVC = [[FriendSetViewController alloc] init];
    friendSetVC.markName = self.friendApplyModel.name;
    [self.navigationController pushViewController:friendSetVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 动态高度
    NewFriendTableViewCell *cell = (NewFriendTableViewCell *)[self tableView:self.friendApplyTableView cellForRowAtIndexPath:indexPath];
    return cell.liuYanLabel.bottom + 15 * kAdjustParamater;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
